package com.sk.bilibili.apiMapper.bean;

import com.sk.bilibili.apiMapper.interfaces.StringConverter;
import lombok.Data;
import lombok.SneakyThrows;
import org.json.JSONObject;

import java.lang.reflect.Field;

/**
 * @author 邵康
 * @date 2021/7/11 16:47
 */
@Data
public class FieldInfo {

    private Field field;
    private String[] paths;
    private boolean collection;
    private Class clazz;
    private StringConverter converter;
    private boolean array;
    private Class sonClass;
    private int len;

    public void init() {
        field.setAccessible(true);
        array=clazz.isArray();
        if(array)sonClass=clazz.getComponentType();
        len=paths.length-1;
    }

    @SneakyThrows
    public void setValue(Object res, Object value) {
        field.set(res,value);
    }

    public JSONObject movePath(JSONObject object) {
        for (int i = 0; i < len; i++) {
            object=object.getJSONObject(paths[i]);
        }
        return object;
    }

    public String getLastPath() {
        return paths[len];
    }

}
